package com.dycx.food.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.common.ExcelUtils;
import com.dycx.common.RestResponse;
import com.dycx.common.enums.BizTypeEnum;
import com.dycx.common.vendor.oss.AliyunOss;
import com.dycx.food.dto.ShoppingCartDTO;
import com.dycx.food.mapper.GoodsFoodMapper;
import com.dycx.food.dto.GoodsDTO;
import com.dycx.food.dto.GoodsQueryDTO;
import com.dycx.food.dto.GoodsSpecDTO;
import com.dycx.food.entity.GoodsDO;
import com.dycx.food.entity.GoodsMainDO;
import com.dycx.food.entity.GoodsSpecValueDO;
import com.dycx.food.service.GoodsClassFoodService;
import com.dycx.food.service.GoodsMainFoodService;
import com.dycx.food.service.GoodsFoodService;
import com.dycx.food.service.GoodsSpecValueFoodService;
import com.dycx.food.vo.GoodsSpecVO;
import com.dycx.food.vo.GoodsSpecValueVO;
import com.dycx.food.vo.GoodsVO;
import com.dycx.food.vo.ShoppingCartVO;
import com.dycx.system.dto.LogCreateDTO;
import com.dycx.system.service.LogService;
import com.dycx.system.service.OssFileService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class GoodsFoodImpl extends ServiceImpl<GoodsFoodMapper, GoodsDO> implements GoodsFoodService {

    private GoodsFoodMapper goodsMapper;
    private GoodsMainFoodService goodsMainService;
    private GoodsSpecValueFoodService goodsSpecValueService;
    private LogService logService;
    private OssFileService ossFileService;
    private GoodsClassFoodService goodsClassService;


    @Override
    public Boolean deleteGoods(int mainGoodsId) {
        LambdaQueryWrapper<GoodsDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsDO::getMainGoodsId, mainGoodsId);

        GoodsMainDO oldGoodsMainDO = goodsMainService.getById(mainGoodsId);
        List<GoodsDO> oldGoodsList = this.list(queryWrapper);

        if (!this.remove(queryWrapper)) return false;
        if (!goodsMainService.removeById(mainGoodsId)) return false;

        oldGoodsList.forEach(item -> {
            ossFileService.setDeleted(item.getThumbnail());
            ossFileService.setDeleted(item.getBanner());
        });
        ossFileService.setDeleted(oldGoodsMainDO.getGoodsDetail(), true);

        return true;
    }

    /**
     * 添加商品
     *
     * @param goodsDto
     * @return
     */
    @Override
    public Boolean addGoods(GoodsDTO goodsDto) {
        goodsDto.setGoodsDetail(AliyunOss.delDomain(ossFileService.regexBase64Upload(goodsDto.getGoodsDetail()), true));
        goodsDto.getGoodsSpecList().forEach(item -> {
            item.setThumbnail(AliyunOss.delDomain(item.getThumbnail()));
            item.setBanner(AliyunOss.delDomain(item.getBanner()));
        });

        //计算总商品信息
        int minPrice = 0;
        int minVipPrice = 0;
        int totalStorage = 0;

        List<GoodsSpecDTO> goodsSpecList = goodsDto.getGoodsSpecList();
        for (GoodsSpecDTO goodsSpecDto : goodsSpecList) {
            if (minPrice == 0 || minPrice > goodsSpecDto.getGoodsPrice()) {
                minPrice = goodsSpecDto.getGoodsPrice();
            }

            if (minVipPrice == 0 || minVipPrice > goodsSpecDto.getGoodsVipPrice()) {
                minVipPrice = goodsSpecDto.getGoodsVipPrice();
            }

            totalStorage += goodsSpecDto.getGoodsStorage();
        }

        int maxSort = goodsMainService.getMaxSort();

        //添加主商品
        GoodsMainDO goodsMainDO = new GoodsMainDO();
        goodsMainDO.setGoodsClassId(goodsDto.getGoodsClassId());
        goodsMainDO.setBrandId(goodsDto.getBrandId());
        goodsMainDO.setDesignId(goodsDto.getDesignId());
        goodsMainDO.setGoodsName(goodsDto.getGoodsName());
        goodsMainDO.setGoodsDescription(goodsDto.getGoodsDescription());
        goodsMainDO.setGoodsPrice(minPrice);
        goodsMainDO.setGoodsVipPrice(minVipPrice);
        goodsMainDO.setGoodsStorage(totalStorage);
        goodsMainDO.setIsBatchUpdate(goodsDto.getIsBatchUpdate());
        goodsMainDO.setGoodsDetail(goodsDto.getGoodsDetail());
        goodsMainDO.setSort(maxSort + 1);

        boolean res = goodsMainService.save(goodsMainDO);
        if (!res) throw new RuntimeException("添加公共商品失败");

        //添加商品规格和参数
        goodsDto.getGoodsSpecValueList().forEach(item -> {
            GoodsSpecValueDO goodsSpecValueDO = BeanUtil.toBean(item, GoodsSpecValueDO.class);
            goodsSpecValueDO.setMainGoodsId(goodsMainDO.getId());

            goodsSpecValueService.save(goodsSpecValueDO);
        });

        //添加不同规格的商品
        for (int i = 0; i < goodsSpecList.size(); i++) {
            GoodsSpecDTO goodsSpecDto = goodsSpecList.get(i);

            GoodsDO goodsDO = new GoodsDO();
            goodsDO.setMainGoodsId(goodsMainDO.getId());
            goodsDO.setGoodsClassId(goodsDto.getGoodsClassId());
            goodsDO.setBrandId(goodsDto.getBrandId());
            goodsDO.setDesignId(goodsDto.getDesignId());
            if (goodsSpecDto.getGoodsName().equals("")) {
                goodsDO.setGoodsName(goodsDto.getGoodsName() + " " + goodsSpecDto.getSpecKey().replace("-", " "));
            } else {
                goodsDO.setGoodsName(goodsSpecDto.getGoodsName());
            }
            goodsDO.setGoodsDescription(goodsDto.getGoodsDescription());
            goodsDO.setSpecKey(goodsSpecDto.getSpecKey());
            goodsDO.setGoodsPrice(goodsSpecDto.getGoodsPrice());
            goodsDO.setGoodsVipPrice(goodsSpecDto.getGoodsVipPrice());
            goodsDO.setGoodsStorage(goodsSpecDto.getGoodsStorage());
            goodsDO.setGoodsSerial(goodsSpecDto.getGoodsSerial());
            goodsDO.setIsDefaultCommon(goodsSpecDto.getIsDefaultCommon());
            goodsDO.setSort(i);
            goodsDO.setThumbnail(goodsSpecDto.getThumbnail());
            goodsDO.setBanner(goodsSpecDto.getBanner());

            this.save(goodsDO);

            if (goodsSpecDto.getIsDefaultCommon()) {
                goodsMainDO.setThumbnail(goodsSpecDto.getThumbnail());
                goodsMainDO.setBanner(goodsSpecDto.getBanner());
                goodsMainService.updateById(goodsMainDO);
            }

            ossFileService.setUse(goodsSpecDto.getThumbnail());
            ossFileService.setUse(goodsSpecDto.getBanner());
        }

        ossFileService.setUse(goodsDto.getGoodsDetail(), true);

        goodsDto.setId(goodsMainDO.getId());
        return true;
    }

    /**
     * 更新商品
     *
     * @param goodsDto
     * @return
     */
    @Override
    public Boolean updateGoods(GoodsDTO goodsDto) {
        goodsDto.setGoodsDetail(AliyunOss.delDomain(ossFileService.regexBase64Upload(goodsDto.getGoodsDetail())));
        goodsDto.getGoodsSpecList().forEach(item -> {
            item.setThumbnail(AliyunOss.delDomain(item.getThumbnail()));
            item.setBanner(AliyunOss.delDomain(item.getBanner()));
        });

        //计算总商品信息
        int minPrice = 0;
        int minVipPrice = 0;
        int totalStorage = 0;

        //用于删除图片
        GoodsMainDO oldGoodsMainDO = goodsMainService.getById(goodsDto.getId());

        List<GoodsSpecDTO> goodsSpecList = goodsDto.getGoodsSpecList();
        for (GoodsSpecDTO goodsSpecDto : goodsSpecList) {
            if (minPrice == 0 || minPrice > goodsSpecDto.getGoodsPrice()) {
                minPrice = goodsSpecDto.getGoodsPrice();
            }

            if (minVipPrice == 0 || minVipPrice > goodsSpecDto.getGoodsVipPrice()) {
                minVipPrice = goodsSpecDto.getGoodsVipPrice();
            }

            totalStorage += goodsSpecDto.getGoodsStorage();
        }

        //修改总商品
        GoodsMainDO goodsMainDO = new GoodsMainDO();
        goodsMainDO.setId(goodsDto.getId());
        goodsMainDO.setGoodsClassId(goodsDto.getGoodsClassId());
        goodsMainDO.setBrandId(goodsDto.getBrandId());
        goodsMainDO.setDesignId(goodsDto.getDesignId());
        goodsMainDO.setGoodsName(goodsDto.getGoodsName());
        goodsMainDO.setGoodsDescription(goodsDto.getGoodsDescription());
        goodsMainDO.setGoodsPrice(minPrice);
        goodsMainDO.setGoodsVipPrice(minVipPrice);
        goodsMainDO.setGoodsStorage(totalStorage);
        goodsMainDO.setIsBatchUpdate(goodsDto.getIsBatchUpdate());
        goodsMainDO.setGoodsDetail(goodsDto.getGoodsDetail());

        boolean res = goodsMainService.updateById(goodsMainDO);

        if (!res) {
            throw new RuntimeException("添加公共商品失败");
        }

        ossFileService.setDeleted(oldGoodsMainDO.getGoodsDetail(), true);
        ossFileService.setUse(goodsDto.getGoodsDetail(), true);

        List<GoodsSpecVO> originList = this.getListByMainGoodsId(goodsMainDO.getId());
        originList.forEach(item -> {
            ossFileService.setDeleted(item.getThumbnail());
            ossFileService.setDeleted(item.getBanner());
        });

        //删除所有参数
        LambdaQueryWrapper<GoodsSpecValueDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsSpecValueDO::getMainGoodsId, goodsMainDO.getId());
        goodsSpecValueService.remove(wrapper);

        //添加商品参数
        goodsDto.getGoodsSpecValueList().forEach(item -> {
            GoodsSpecValueDO goodsSpecValueDO = BeanUtil.toBean(item, GoodsSpecValueDO.class);
            goodsSpecValueDO.setMainGoodsId(goodsMainDO.getId());
            goodsSpecValueService.save(goodsSpecValueDO);
        });

        //添加不同规格的商品
        for (int i = 0; i < goodsSpecList.size(); i++) {
            GoodsSpecDTO goodsSpecDto = goodsSpecList.get(i);

            GoodsDO goodsDO = new GoodsDO();
            goodsDO.setMainGoodsId(goodsMainDO.getId());
            goodsDO.setGoodsClassId(goodsDto.getGoodsClassId());
            goodsDO.setBrandId(goodsDto.getBrandId());
            goodsDO.setDesignId(goodsDto.getDesignId());

            if ("".equals(goodsSpecDto.getGoodsName())) {
                goodsDO.setGoodsName(goodsDto.getGoodsName() + " " + goodsSpecDto.getSpecKey().replace("-", " "));
            } else {
                goodsDO.setGoodsName(goodsSpecDto.getGoodsName());
            }

            goodsDO.setGoodsDescription(goodsDto.getGoodsDescription());
            goodsDO.setSpecKey(goodsSpecDto.getSpecKey());
            goodsDO.setGoodsPrice(goodsSpecDto.getGoodsPrice());
            goodsDO.setGoodsVipPrice(goodsSpecDto.getGoodsVipPrice());
            goodsDO.setGoodsStorage(goodsSpecDto.getGoodsStorage());
            goodsDO.setGoodsSerial(goodsSpecDto.getGoodsSerial());
            goodsDO.setIsBatchUpdate(goodsDto.getIsBatchUpdate());
            goodsDO.setIsDefaultCommon(goodsSpecDto.getIsDefaultCommon());
            goodsDO.setSort(i);
            goodsDO.setThumbnail(goodsSpecDto.getThumbnail());
            goodsDO.setBanner(goodsSpecDto.getBanner());

            if (goodsSpecDto.getId() != null && goodsSpecDto.getId() > 0) {
                goodsDO.setId(goodsSpecDto.getId());
                this.updateById(goodsDO);

                //删除集合中指定元素
                originList.removeIf(goodsSpecVO -> goodsSpecVO.getId().equals(goodsDO.getId()));
            } else {
                this.save(goodsDO);
            }

            if (goodsSpecDto.getIsDefaultCommon()) {
                goodsMainDO.setThumbnail(goodsSpecDto.getThumbnail());
                goodsMainDO.setBanner(goodsSpecDto.getBanner());
                goodsMainService.updateById(goodsMainDO);
            }

            ossFileService.setUse(goodsSpecDto.getThumbnail());
            ossFileService.setUse(goodsSpecDto.getBanner());
        }

        //删除规格
        originList.forEach(item -> {
            this.removeById(item.getId());
        });

        return true;
    }

    /**
     * 更新上架状态
     *
     * @param goodsDto
     * @return
     */
    @Override
    public Boolean updateShelf(GoodsDTO goodsDto) {
        GoodsMainDO goodsMainDO = new GoodsMainDO();
        goodsMainDO.setId(goodsDto.getId());
        goodsMainDO.setIsShelf(goodsDto.getIsShelf());
        goodsMainService.updateById(goodsMainDO);

        LambdaQueryWrapper<GoodsDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsDO::getMainGoodsId, goodsDto.getId());

        GoodsDO goodsDO = new GoodsDO();
        goodsDO.setIsShelf(goodsDto.getIsShelf());

        return this.update(goodsDO, queryWrapper);
    }

    @Override
    public Boolean batchUpdateGoodsPrice(HttpServletRequest request) {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartHttpServletRequest.getFile("goodsPriceFile");
        float priceRange = Float.parseFloat(multipartHttpServletRequest.getParameter("priceRange").trim());
        int calcType = Integer.parseInt(multipartHttpServletRequest.getParameter("calcType").trim());
        if (file.isEmpty()) {
            return false;
        }
        try {
            InputStream inputStream = file.getInputStream();
            List<List<List<String>>> list = ExcelUtils.getCourseListByExcel(inputStream, file.getOriginalFilename());
            inputStream.close();

            //清空所有库存
            GoodsDO goodsDOClear = new GoodsDO();
            goodsDOClear.setGoodsStorage(0);
            goodsDOClear.setGoodsPrice(0);
            goodsDOClear.setGoodsVipPrice(0);
            this.update(goodsDOClear, null);

            GoodsMainDO goodsMainDO = new GoodsMainDO();
            goodsMainDO.setGoodsStorage(0);
            goodsMainDO.setGoodsPrice(0);
            goodsMainDO.setGoodsVipPrice(0);
            goodsMainService.update(goodsMainDO, null);

            for (List<List<String>> sheet : list) {
                for (List<String> row : sheet) {
                    for (int i = 0; i < row.size(); i++) {
                        String col = row.get(i).trim();
                        //最后一列不查询
                        if (i == row.size() - 1) {
                            break;
                        }

                        //当前数据是否符合条件
                        if (col.equals("") || col.length() <= 8 || col.length() > 100) {
                            continue;
                        }

                        //下一个单元格是否符合条件
                        String nextCol = row.get(i + 1).trim();
                        if (nextCol.equals("") || nextCol.length() > 8 || Float.parseFloat(nextCol) < 0) {
                            continue;
                        }

                        //根据商品名称查询商品是否存在
                        List<GoodsVO> goodsVOList = goodsMapper.getGoodsInfoByName(col);
                        if (goodsVOList == null) {
                            continue;
                        }

                        if (goodsVOList.size() > 1) {
                            throw new RuntimeException("商品名称【" + col + "】重复，导入终止，请修改后重新导入");
                        }

                        if (goodsVOList.size() < 1) {
                            continue;
                        }

                        GoodsDO goodsDO = BeanUtil.toBean(goodsVOList.get(0), GoodsDO.class);

                        if (!goodsDO.getIsBatchUpdate()) {
                            continue;
                        }

                        //修改商品价格
                        float nextColPrice = Float.parseFloat(nextCol);
                        if (nextColPrice > 0) { //价格不为0，修改价格
                            int currentPrice = (int) (Float.parseFloat(nextCol) * 100);
                            goodsDO.setGoodsVipPrice(currentPrice);
                            if (calcType == 1) {
                                goodsDO.setGoodsPrice((int) (currentPrice + (priceRange * 100)));
                            } else {
                                goodsDO.setGoodsPrice((int) (currentPrice * (1 + priceRange)));
                            }
                            if (goodsDO.getGoodsStorage() == 0) {
                                goodsDO.setGoodsStorage(999);
                            }

                            this.updateById(goodsDO);
                        }
                    }
                }
            }

            this.syncMainGoodsPrice();

            LogCreateDTO dto = new LogCreateDTO();
            dto.setRequest(request);
            dto.setPost(JSON.toJSONString(list));
            dto.setResponse(RestResponse.success());
            dto.setBizType(BizTypeEnum.MALL_GOODS.getCode());
            dto.setLogTitle("批量导入端口价格");
            logService.addLog(dto);

            return true;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 分页
     *
     * @return
     */
    @Override
    public Page<GoodsVO> getPage(GoodsDTO goodsDto) {
        Page<GoodsVO> page = new Page<>(goodsDto.getPageIndex(), goodsDto.getPageSize());  // 查询第1页，每页返回5条
        page = goodsMapper.getPage(page, goodsDto);
        page.getRecords().forEach(item -> {
            item.setThumbnail(AliyunOss.addDomain(item.getThumbnail()));
            item.setCommonThumbnail(AliyunOss.addDomain(item.getCommonThumbnail()));
            item.setBanner(AliyunOss.addDomain(item.getBanner()));
        });
        return page;
    }

    /**
     * 分页
     *
     * @return
     */
    @Override
    public Page<GoodsVO> getSearchGoodsPage(GoodsDTO goodsDto) {
        Page<GoodsVO> page = new Page<>(goodsDto.getPageIndex(), goodsDto.getPageSize());  // 查询第1页，每页返回5条
        page = goodsMapper.getSearchGoodsPage(page, goodsDto);
        page.getRecords().forEach(item -> {
            item.setThumbnail(AliyunOss.addDomain(item.getThumbnail()));
            item.setCommonThumbnail(AliyunOss.addDomain(item.getCommonThumbnail()));
            item.setBanner(AliyunOss.addDomain(item.getBanner()));
        });
        return page;
    }

    /**
     * 获取公共商品下的规格商品
     *
     * @param mainGoodsId
     * @return
     */
    @Override
    public List<GoodsSpecVO> getListByMainGoodsId(Integer mainGoodsId) {

        LambdaQueryWrapper<GoodsDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsDO::getMainGoodsId, mainGoodsId);
        queryWrapper.orderByAsc(GoodsDO::getSort);
        List<GoodsDO> goodsList = this.list(queryWrapper);

        List<GoodsSpecVO> voList = goodsList.stream().map(item ->
                BeanUtil.toBean(item, GoodsSpecVO.class)
        ).collect(Collectors.toList());

        voList.forEach(item -> {
            item.setThumbnail(AliyunOss.addDomain(item.getThumbnail()));
            item.setBanner(AliyunOss.addDomain(item.getBanner()));
        });

        return voList;
    }

    @Override
    public GoodsVO getGoodsMainDetail(int mainGoodsId) {
        GoodsMainDO goodsMainDO = goodsMainService.getById(mainGoodsId);

        GoodsVO goodsVo = BeanUtil.toBean(goodsMainDO, GoodsVO.class);
        goodsVo.setGoodsSpecList(this.getListByMainGoodsId(goodsVo.getId()));

        LambdaQueryWrapper<GoodsSpecValueDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsSpecValueDO::getMainGoodsId, mainGoodsId);

        List<GoodsSpecValueDO> goodsParam = goodsSpecValueService.list(wrapper);

        List<GoodsSpecValueVO> goodsParamVo = goodsParam.stream().map(item ->
                BeanUtil.toBean(item, GoodsSpecValueVO.class)
        ).collect(Collectors.toList());

        goodsVo.setGoodsSpecValueList(goodsParamVo);
        goodsVo.setGoodsClassIdLevel(goodsClassService.getGoodsClassIdLevel(goodsVo.getGoodsClassId()));

        goodsVo.setThumbnail(AliyunOss.addDomain(goodsVo.getThumbnail()));
        goodsVo.setCommonThumbnail(AliyunOss.addDomain(goodsVo.getCommonThumbnail()));
        goodsVo.setBanner(AliyunOss.addDomain(goodsVo.getBanner()));
        goodsVo.setGoodsDetail(AliyunOss.addDomain(goodsVo.getGoodsDetail(), true));

        return goodsVo;
    }

    /**
     * 同步公共商品价格
     */
    public void syncMainGoodsPrice() {
        List<GoodsMainDO> commonEntityList = goodsMainService.list();

        for (GoodsMainDO goodsMainDO : commonEntityList) {
            LambdaQueryWrapper<GoodsDO> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(GoodsDO::getMainGoodsId, goodsMainDO.getId())
                    .gt(GoodsDO::getGoodsPrice, 0)
                    .orderByAsc(GoodsDO::getGoodsPrice);

            List<GoodsDO> goodsDOList = this.list(wrapper);

            if (goodsDOList != null && goodsDOList.size() > 0) {
                int totalStorage = 0;
                for (GoodsDO goodsDO : goodsDOList) {
                    totalStorage += goodsDO.getGoodsStorage();
                }

                goodsMainDO.setGoodsPrice(goodsDOList.get(0).getGoodsPrice());
                goodsMainDO.setGoodsVipPrice(goodsDOList.get(0).getGoodsVipPrice());
                goodsMainDO.setGoodsStorage(totalStorage);

                goodsMainService.updateById(goodsMainDO);
            }
        }
    }

    /**
     * 热销商品
     *
     * @param num
     * @return
     */
    @Override
    public List<GoodsVO> listSalesGoods(int num) {
        LambdaQueryWrapper<GoodsMainDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.gt(GoodsMainDO::getGoodsPrice, 0);
        wrapper.gt(GoodsMainDO::getGoodsStorage, 0);
        wrapper.eq(GoodsMainDO::getIsShelf, true);
        wrapper.orderByDesc(GoodsMainDO::getSales);

        List<GoodsMainDO> mainGoodsList = goodsMainService.list(wrapper);
        List<GoodsVO> voList = mainGoodsList.stream().map(item ->
                BeanUtil.toBean(item, GoodsVO.class)
        ).collect(Collectors.toList());

        voList.forEach(item -> {
            item.setThumbnail(AliyunOss.addDomain(item.getThumbnail()));
            item.setCommonThumbnail(AliyunOss.addDomain(item.getCommonThumbnail()));
            item.setBanner(AliyunOss.addDomain(item.getBanner()));
        });

        return voList;
    }

    /**
     * 查询购物车下的商品信息
     *
     * @param list
     * @return
     */
    @Override
    public List<ShoppingCartVO> listGoodsByOrder(List<ShoppingCartDTO> list) {
        List<ShoppingCartVO> goodsList = new ArrayList<>();
        list.forEach(item -> {
            GoodsDO goodsDO = this.getById(item.getGoodsId());

            ShoppingCartVO shoppingCartVO = new ShoppingCartVO();
            shoppingCartVO.setGoodsId(goodsDO.getId());
            shoppingCartVO.setGoodsNum(item.getGoodsNum());
            shoppingCartVO.setGoodsName(goodsDO.getGoodsName());
            shoppingCartVO.setGoodsPrice(goodsDO.getGoodsPrice());
            shoppingCartVO.setGoodsVipPrice(goodsDO.getGoodsVipPrice());

            goodsList.add(shoppingCartVO);
        });

        return goodsList;
    }

    /**
     * 主商品分页
     *
     * @param dto
     * @return
     */
    @Override
    public Page<GoodsVO> getGoodsMainPage(GoodsQueryDTO dto) {
        Page<GoodsVO> goodsMainList = goodsMainService.getPage(dto);

        if (ObjectUtil.isNotNull(dto.getIsQueryChild()) && dto.getIsQueryChild()) {
            goodsMainList.getRecords().forEach(item -> {
                item.setGoodsSpecList(this.getListByMainGoodsId(item.getId()));
            });
        }

        goodsMainList.getRecords().forEach(item -> {
            item.setThumbnail(AliyunOss.addDomain(item.getThumbnail()));
            item.setCommonThumbnail(AliyunOss.addDomain(item.getCommonThumbnail()));
            item.setBanner(AliyunOss.addDomain(item.getBanner()));
        });

        return goodsMainList;
    }


}

